package QianFeng07.IODemo;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 字节字符转换流
 * 转换流，可以将一个字节流转换为字符流，也可以将一个字符流转换为字节流。
 * OutputStreamWriter:可以将输出的字符流转换为字节流的输出形式
 * InputStreamReader: 将输入的字节流转换为字符流输入形式
 **/
public class ChangeStreamDemo {
    public static void main(String[] args) throws FileNotFoundException {
        //读取(输入)
        InputStream inputStream = new FileInputStream("F:\\Test\\Test.txt");
        read(inputStream);
        //写出（输出）
        OutputStream outputStream = new FileOutputStream("F:\\Test\\Test.txt");
        write(outputStream);
    }

    private static void write(OutputStream outputStream) {
        Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
        try {
            writer.write("开开心心的玩耍");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void read(InputStream inputStream) {
        Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        char[] cs = new char[1024];
        int len = -1;
        try {
            while ((len = reader.read(cs)) != -1)
                System.out.println(new String(cs, 0, len));
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
